<?php

namespace app\admin\model;

use think\Model;


class Phone extends Model
{

    

    

    // 表名
    protected $name = 'phone';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'outbound_enum_text',
        'sales_enum_text',
        'displace_enum_text',
        'accessory_set_text',
        'pay_enum_text',
        'c_sex_text',
        'follow_enum_text',
        'gift_enum_text',
        'delete_time_text'
    ];
    

    
    public function getOutboundEnumList()
    {
        return ['0' => __('Outbound_enum 0'), '1' => __('Outbound_enum 1'), '2' => __('Outbound_enum 2')];
    }

    public function getSalesEnumList()
    {
        return ['0' => __('Sales_enum 0'), '1' => __('Sales_enum 1'), '2' => __('Sales_enum 2')];
    }

    public function getDisplaceEnumList()
    {
        return ['0' => __('Displace_enum 0'), '1' => __('Displace_enum 1')];
    }

    public function getAccessorySetList()
    {
        return ['0' => __('Accessory_set 0'), '1' => __('Accessory_set 1'), '2' => __('Accessory_set 2')];
    }

    public function getPayEnumList()
    {
        return ['0' => __('Pay_enum 0'), '1' => __('Pay_enum 1'), '2' => __('Pay_enum 2'), '3' => __('Pay_enum 3')];
    }

    public function getCSexList()
    {
        return ['1' => __('C_sex 1'), '0' => __('C_sex 0')];
    }

    public function getFollowEnumList()
    {
        return ['0' => __('Follow_enum 0'), '1' => __('Follow_enum 1')];
    }

    public function getGiftEnumList()
    {
        return ['0' => __('Gift_enum 0'), '1' => __('Gift_enum 1')];
    }


    public function getOutboundEnumTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['outbound_enum']) ? $data['outbound_enum'] : '');
        $list = $this->getOutboundEnumList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSalesEnumTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sales_enum']) ? $data['sales_enum'] : '');
        $list = $this->getSalesEnumList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDisplaceEnumTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['displace_enum']) ? $data['displace_enum'] : '');
        $list = $this->getDisplaceEnumList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAccessorySetTextAttr($value, $data)
    {
        $value = $value ?: ($data['accessory_set'] ?? '');
        $valueArr = explode(',', $value);
        $list = $this->getAccessorySetList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }


    public function getPayEnumTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_enum']) ? $data['pay_enum'] : '');
        $list = $this->getPayEnumList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCSexTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['c_sex']) ? $data['c_sex'] : '');
        $list = $this->getCSexList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getFollowEnumTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['follow_enum']) ? $data['follow_enum'] : '');
        $list = $this->getFollowEnumList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getGiftEnumTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['gift_enum']) ? $data['gift_enum'] : '');
        $list = $this->getGiftEnumList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDeleteTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['delete_time']) ? $data['delete_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setAccessorySetAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    protected function setDeleteTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function store()
    {
        return $this->belongsTo('Store', 's_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function admin()
    {
        return $this->belongsTo('Admin', 'a_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
